package com.cdp.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdp.common.core.domain.AjaxResult;
import com.cdp.common.core.domain.entity.SysSand;
import com.cdp.system.dto.SandDTO;
import com.cdp.system.mapper.SysSandMapper;
import com.cdp.system.service.ISysSandService;
import com.cdp.system.vo.IdsVO;
import com.cdp.system.vo.SysSandVO;
import org.apache.commons.math3.geometry.partitioning.AbstractRegion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 砂服务实现类
 */
@Service
public class SysSandServiceImpl extends ServiceImpl<SysSandMapper, SysSand> implements ISysSandService {

    @Autowired
    private SysSandMapper sysSandMapper;

    /**
     * 查询砂集合
     *
     * @return
     */
    @Override
    public List<SysSand> listSand() {
        List<SysSand> list = list();
        return list;
    }


    /**
     * 查询砂信息
     *
     * @param sandDTO 查询条件
     * @return 砂信息列表
     */
    @Override
    public AjaxResult listMistedSand(SandDTO sandDTO) {

     List<SysSandVO>  sysSandVOS= sysSandMapper.listMistedSand(sandDTO);

        return AjaxResult.success(sysSandVOS);
    }


    /**
     * 根据id模糊查询
     * @param id
     * @return
     */
    @Override
    public List<IdsVO> querySandIdsById(Integer id) {
        // 创建查询条件
        LambdaQueryWrapper<SysSand> wrapper = new LambdaQueryWrapper<>();

        // 如果 id 不为空，则添加模糊查询条件
        if (id != null) {
            wrapper.like(SysSand::getSandId, id);
        }

        // 查询数据库
        List<SysSand> sysSands = sysSandMapper.selectList(wrapper);

        // 转换为 IdsVO 对象
        IdsVO idsVO = new IdsVO();
        List<Integer> sandIds = sysSands.stream()
                .map(SysSand::getSandId)
                .collect(Collectors.toList());
        idsVO.setIds(sandIds);

        return Collections.singletonList(idsVO);
    }

    @Override
    public List<SysSand> listSandByIds(List<Integer> sandIds) {
        if (sandIds == null || sandIds.isEmpty()) {
            return Collections.emptyList();
        }
       List<SysSand> sandList= sysSandMapper.listSandByIds(sandIds);
        return sandList;
    }


    /**
     * 查询砂最大值最小值
     * @return
     */
    @Override
    public Map<String, Object> querySandMaxMin() {
        return sysSandMapper.querySandMaxMin();
    }


}
